package com.framework.loippi.controller.api;

import com.framework.loippi.api.utils.ApiUtils;
import com.framework.loippi.entity.BbsFile;
import com.framework.loippi.entity.BbsMsg;
import com.framework.loippi.entity.TBbs;
import com.framework.loippi.service.BbsFileService;
import com.framework.loippi.service.BbsMsgService;
import com.framework.loippi.service.TBbsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName BbsController
 * @Description TODO
 * @Author fsociety
 * @Date 28/12/2019 下午5:57
 * @Version 1.0
 */

@Controller("apiBbsController")
@RequestMapping("/api/bbs")
@Api(tags = {"论坛信息"})
public class BbsController extends ApiBaseController {

    @Autowired
    TBbsService bbsService;
    @Autowired
    BbsFileService bbsFileService;
    @Autowired
    BbsMsgService bbsMsgService;

    @RequestMapping(value = "/generate.json", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value = "发布论坛", notes = "{}")
    @Transactional(rollbackFor = Exception.class)
    public @ResponseBody
    String generate(Long userId, String content, String imageArr) {
        TBbs bbs = new TBbs();
        bbs.setUserId(userId);
        bbs.setContent(content);
        bbs.setCreateTime(new Date());
        bbs.setUpdateTime(new Date());
        bbsService.save(bbs);
        if (imageArr != null) {
            JSONArray jsonArray = JSONArray.fromObject(imageArr);
            for (Object o : jsonArray) {
                BbsFile bbsFile = new BbsFile();
                bbsFile.setBbsId(bbs.getId());
                bbsFile.setUrl((String) o);
                bbsFile.setCreateTime(new Date());
                bbsFileService.save(bbsFile);
            }
        }

        return ApiUtils.success();
    }

    @RequestMapping(value = "/comment.json", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value = "评论回复", notes = "{}")
    public @ResponseBody
    String comment(Long bbsId, Long commentId, Long commentedId, String content, String commentUserName, String commentedUserName) {
        if (null != content && "" != content) {
            BbsMsg bm = new BbsMsg();
            bm.setBbsId(bbsId);
            bm.setCommentUserId(commentId);
            bm.setCommentedUserId(commentedId);
            bm.setCommentUserName(commentUserName);
            bm.setCommentedUserName(commentedUserName);
            bm.setComment(content);
            bm.setCreateTime(new Date());
            bbsMsgService.save(bm);
        }
        return ApiUtils.success();
    }

    @RequestMapping(value = "/findBbs.json", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value = "查询论坛", notes = "{}")
    public @ResponseBody
    String findBbs(Long userId, @Param("currentPage") Integer currentPage) {
        List<TBbs> list;
        Map<String, Object> map = new HashMap<>();
        int pageSize = 5;
        int cur = (currentPage - 1) * pageSize;
        Long total = 0L;
        if (userId == null) {
            list = bbsService.findAllByPage(cur, pageSize);
            total = bbsService.count();
        } else {
            list = bbsService.findByUserId(userId, cur, pageSize);
            Map<String, Object> param = new HashMap<>();
            map.put("userId", userId);
            total = bbsService.count(map);
        }

        for (TBbs tb : list) {
            tb.setImages(bbsFileService.findFileByBbsId(tb.getId()));
            tb.setBbsMsgs(bbsMsgService.findAllByBbsId(tb.getId()));
        }

        map.put("list", list);
        map.put("total", total);
        return ApiUtils.success(map);
    }
}
